package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.IInterfaceForm;
import com.shimao.celnet.payment.mapper.IAppInterfaceMapper;
import com.shimao.celnet.payment.mapper.IInterfaceMapper;
import com.shimao.celnet.payment.model.IAppInterface;
import com.shimao.celnet.payment.model.IInterface;
import com.shimao.celnet.payment.service.IInterfaceService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;


/**
 * 接口注册 管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class IInterfaceServiceImpl extends BaseServiceImpl<IInterface> implements IInterfaceService {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired(required = false)
    private IInterfaceMapper localMapper;

    @Autowired(required = false)
    private IAppInterfaceMapper iAppInterfaceMapper;

    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public IInterfaceForm getByPage(IInterfaceForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");

        //分页查询
        Example example = new Example(IInterface.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(formBean.getInterfaceId())) {
            //接口编号
            criteria.andLike("interfaceId", "%" + formBean.getInterfaceId() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getInterfaceName())) {
            //接口名称
            criteria.andLike("interfaceName", "%" + formBean.getInterfaceName() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getInterfaceApi())) {
            //接口api
            criteria.andLike("interfaceApi", "%" + formBean.getInterfaceApi() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getInterfaceType())) {
            //类型
            criteria.andLike("interfaceType", "%" + formBean.getInterfaceType() + "%");
        }

        example.orderBy("interfaceId");
        //分页查询
        PageHelper.startPage(pageNum, length);
        List<IInterface> tableList = getByExample(example);
        formBean.setPageInfo(tableList);

        return  formBean;
    }

    /**
     *新增权限保存
     * @return
     */
    @Override
    public int save(IInterface sBean){
        int retCnt=0;
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setGuid(getGuid());//guid

        String interfaceUrl = sBean.getInterfaceUrl();
        if(!FinalUtil.PERMISSION_MENU_BIAS.equals(StringUtils.left(interfaceUrl,1))){
            sBean.setInterfaceUrl(FinalUtil.PERMISSION_MENU_BIAS+interfaceUrl);
        }
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改权限保存
     * @return
     */
    @Override
    public int update(IInterface sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        String interfaceUrl = sBean.getInterfaceUrl();
        if(!FinalUtil.PERMISSION_MENU_BIAS.equals(StringUtils.left(interfaceUrl,1))){
            sBean.setInterfaceUrl(FinalUtil.PERMISSION_MENU_BIAS+interfaceUrl);
        }
        int retCnt = super.update(sBean);

        return retCnt;
    }

    /**
     * 根据guid，删除信息
     * @param interfaceGuid
     */
    @Override
    @CacheEvict(cacheNames="interfaces", allEntries=true)
    @Transactional(propagation= Propagation.REQUIRES_NEW,readOnly=false,rollbackFor={Exception.class})
    public int delete(String interfaceGuid) {
        int retCnt = 0;
        logger.info("删除：根据接口guid，删除系统接口");
        Example example = new Example(IAppInterface.class);
        Example.Criteria criteria = example.createCriteria();
        //系统guid
        criteria.andEqualTo("interfaceGuid", interfaceGuid );
        iAppInterfaceMapper.deleteByExample(example);

        //删除应用系统
        logger.info("删除：根据guid，删除接口");
        retCnt = localMapper.deleteByPrimaryKey(interfaceGuid);
        return retCnt;
    }

}
